﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using SundayWishModel.Entities;
using SundayWishBLL;
public partial class User_planlist : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ContentPlaceHolder UserMaster = (ContentPlaceHolder)Page.Master.Master.FindControl("ContentPlaceHolder1");
            ((Label)UserMaster.FindControl("lbpagename")).Text = "食谱计划";
            ((HyperLink)UserMaster.FindControl("hlplanlist")).CssClass = "select_page";
            DataBind();
            planlistbind();
        }
    }

    private void DataBind()
    {
        Int32 UserId = ((UserInfo)Session["user"]).UserId;
        PlanHelper ph = new PlanHelper();
        IList<Plan> planlist = ph.GetUnSendPlanByUserId(UserId);
        plannumber.InnerText = planlist.Count.ToString();
        gvPlanlist.DataSource = planlist;
        gvPlanlist.DataBind();
    }

    private void planlistbind()
    {
        if (Session["currentplan"] != null)
        {
            PlanHelper ph = new PlanHelper();
            Plan plan = ph.GetPlan(Convert.ToInt32(Session["currentplan"]));
            if (plan != null)
            {
                gvCurrentPlan.DataSource = plan.Dishes;
                gvCurrentPlan.DataBind();
                lbdate.Text = "当前计划日期：" + plan.SendDate.ToLongDateString();
                lbweek.Text = WebHelper.DateToWeek(plan.SendDate);
            }
            else
            {
                clearplanlist();
            }
        }
    }

    protected void current_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("delData"))
        {
            if (Session["currentplan"] != null)
            {
                int DishId = Convert.ToInt32(e.CommandArgument);
                PlanHelper ph = new PlanHelper();
                ph.DeleteDishFromPlan(DishId, Convert.ToInt32(Session["currentplan"]));
            }
        }
        planlistbind();
        DataBind();
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("delData"))
        {
            int PlanId = Convert.ToInt32(e.CommandArgument);
            PlanHelper ph = new PlanHelper();
            ph.DeletePlan(ph.GetPlan(PlanId));
            if (Convert.ToInt32(Session["currentplan"])==PlanId)
            {
                Session.Remove("currentplan");
                clearplanlist();
            }
        }

        if (e.CommandName.Equals("btnDetail"))
        {
            int PlanId = Convert.ToInt32(e.CommandArgument);
            Session["currentplan"]=PlanId;
        }

        DataBind();
        planlistbind();
    }

    private void clearplanlist()
    {
        gvCurrentPlan.DataSource = null;
        gvCurrentPlan.DataBind();
        lbdate.Text = "尚未选择食谱计划";
        lbweek.Text = "";
    }
    protected void lbadd_Click(object sender, EventArgs e)
    {
        if (TextBox1.Text == "")
        {
            WebHelper.AlertMsg("a", "请输入日期");
        }
        else
        {
            PlanHelper ph = new PlanHelper();
            Plan plan = new Plan();
            plan.SendDate = Convert.ToDateTime(TextBox1.Text);
            plan.UserId = ((UserInfo)Session["user"]).UserId;
            plan.Dishes = new List<Dish>();
            if (ph.AddPlan(plan))
            {
                DataBind();
                WebHelper.AlertMsg("ab", "添加成功");
            }
            else
            {
                WebHelper.AlertMsg("abc", "计划已存在或已推送");
            }
        }
    }
}
